package com.etc.controller;


import com.etc.dao.OrderMapper;
import com.etc.dto.Result;
import com.etc.entity.Order;
import com.etc.service.OrderService;
import com.etc.service.PayService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * 涂焯翔
 */

@Controller
@RequestMapping("/order")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayService payService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询某个医生的所有预约情况预约详情
     *
     * @return
     */
    @GetMapping("/detail")
    public @ResponseBody
    Result getOrderDetail() {
        Result result = new Result();
        String doctorId = (String) redisTemplate.opsForValue().get("doctorId");
        if (doctorId != null) {
            return result.ok(orderService.finOrderDetailByDoctorId(doctorId));
        }
        return result;
    }

    /**
     * 查询某个医生当天的预约情况
     *
     * @return
     */
    @GetMapping("/detailpage")
    public @ResponseBody
    Result getOrderDetailByDoctorIdByOrderTimeAsc(@RequestParam int pageNum, @RequestParam int pageSize) {
        Result result = new Result();
        String doctorId = (String) redisTemplate.opsForValue().get("doctorId");
        System.out.println("-----------"+doctorId);
        if (doctorId != null) {
            return result.ok(orderService.finOrderDetailByDoctorIdByPage(doctorId, pageNum, pageSize));
        } else {
            return result;
        }
    }

    /**
     * 获取出诊详细信息
     * @return
     */
    @GetMapping("/orderlist")
    public @ResponseBody
    Result getOrderPatientDoctorPayList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Result result = new Result();
        //Order order = new Order();
        //List<Order> orderPatientDoctorPayList = orderService.findOrderPatientDoctorPayList(order);
        PageInfo<Order> orderList = orderService.findOrderPatientDoctorList(pageNum, pageSize);
        result.ok(orderList);
        return result;
    }


    /**
     * 临时存储患者id
     * @param request
     * @return
     */
    @RequestMapping("/tonewpatient")
    public ModelAndView getOrderDetailByDoctorIdByOrderTimeAsc(HttpServletRequest request) {
        String  patientId =  request.getParameter("patientId");
        if (!patientId.equals("")&&patientId!=null){
            redisTemplate.opsForValue().set("patientId",patientId);
        }
        logger.debug("+++++++++++++++++"+patientId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("new-patient");

        return modelAndView;
    }

    @GetMapping("/PayAndDetailList")
    public @ResponseBody Result  PayAndDetailList(@RequestParam String patientId) {
        Result result=new Result();
        List<Order> payAndDetailList = orderService.getPayAndDetailList(patientId);
        if (!payAndDetailList.isEmpty()){
            return result.ok(payAndDetailList);
        }else {
            return result.fail(0,"没有数据");
        }
    }
    @GetMapping("/getByPatientId")
    public @ResponseBody Result getPatientOrderList(@RequestParam String patientId){
        Result result =new Result();
        Order order =new Order();
        order.setPatientId(patientId);
        List<Order> orderList =orderService.getPatientByPatientId(order);
        if(!orderList.isEmpty()){
            result.ok(orderList);
        }else {
            result.ok(orderList).setMessage("没有相关数据");
        }
        return result;
    }

    /**
     * 根据所传参数doctorId与date，返回该医生从date日起七天时间的预约信息
     * @param doctorId
     * @param date
     * @return
     */
    @GetMapping("/getDoctorOrder")
    public @ResponseBody Result getDoctorOrder(@RequestParam String doctorId,@RequestParam String date){
        Result result = new Result();
        List<Order> orders = orderService.getOrdersByDoctorId(doctorId, date);
        if(!orders.isEmpty()){
            result.ok(orders);
        }else {
            result.fail(1,"未查到该医生有预约记录");
        }

        return result;
    }

    /**
     *
     *
     * @return
     */
    @RequestMapping("/updateOrderStatus")
    public  void updateOrderStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Result result = new Result();
       String orderId = request.getParameter("orderId");
       Integer status = Integer.valueOf(request.getParameter("status")) ;
        Order order = new Order();
        order.setOrderId(orderId);
        if (status == 3) {
            order.setStatus(2);
        } else if (status == 1) {
            order.setStatus(3);
        }
          orderService.updateOrderStatusByOrderId(order);
        response.sendRedirect("http://localhost:8080/doc/index.html");


    }

    @PostMapping("addOrder")
    public @ResponseBody Result addOrder(@RequestParam String patientId,@RequestParam String doctorId,@RequestParam String orderTime,@RequestParam Integer status){
        logger.info("++++++++++++++++++++orderTime:"+orderTime);
        Result result = new Result();
        String orderId = UUID.randomUUID().toString().replaceAll("-","");
        Order order = new Order();
        order.setOrderId(orderId);
        order.setPatientId(patientId);
        order.setDoctorId(doctorId);
        order.setStatus(status);
        order.setOrderTime(orderTime);
        List<Order> orders = new ArrayList<>();
        orders.add(order);

        boolean flag = orderService.addOrder(orders);

        if(flag){
            result.ok(true).setMessage("预约成功！");
        }else {
            result.fail(0,"预约失败！");
        }

        return result;
    }
}
